package com.sw.study.mybatis.practice.annotation;

import com.sw.study.mybatis.practice.pojo.User;
import org.apache.ibatis.annotations.CacheNamespace;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * Created by sw on 2020/3/30.
 */
@CacheNamespace
public interface UserAnnotationMapper {
    @Select("select * from tb_user")
    public List<User> queryByUser();
    @Results({
            @Result(property = "username",column="username"),
            @Result(property = "id",column = "id"),
            @Result(property = "orders",column = "id",javaType = List.class,many = @Many(select="com.sw.study.mybatis.practice.dynamicsql.OrderMapper.selectOrderByUId"))
    })
    @Select("select * from tb_user where id=#{id}")
    public List<User> selectUserAndOrders(Long id);

    /**
     * 查询单个用户
     * @param uid 主键id
     * @return
     */

    @Select("select * from tb_user where id=#{uid}")
    public User selectUserById( Long uid);


    @Results({
            @Result(property = "username",column="username"),
            @Result(property = "id",column = "id"),
            @Result(property = "roles",column = "id",javaType = List.class,many = @Many(select="com.sw.study.mybatis.practice.dynamicsql.RoleMapper.selectRolesByUid"))
    })
    @Select("select * from tb_user ")
    public List<User> selectUsersAndRoles();

}